
<template>
    <!-- 搜素区域 -->
    <el-card class="box-card">
        <el-input placeholder="请输入你想搜索的文章标题" v-model="searchTitle" />
        <el-button type="primary" style="margin-left:20px" @click="search">搜索</el-button>
    </el-card>
    <!-- 表格区域 -->
    <el-card class="box-card" style="margin-top:20px">
        <el-table :data="tableData" border style="width: 100%">
            <el-table-column v-slot="{ row }" label="查看">
                <el-button @click="showVideo(row)" type="warning">审核视频</el-button>
            </el-table-column>
            <el-table-column prop="img" label="封面图" v-slot="{ row }">
                <el-avatar :src="row.img" />
            </el-table-column>
            <el-table-column prop="title" label="标题" v-slot="{ row }">
                <el-tooltip class="box-item" effect="dark" :content="row.title" placement="top-start">
                    <span class="content">{{ row.title }}</span>
                </el-tooltip>

            </el-table-column>
            <el-table-column prop="content" label="内容" v-slot="{ row }">
                <el-tooltip class="box-item" effect="dark" :content="row.content" placement="top-start">
                    <span class="content">{{ row.content }}</span>
                </el-tooltip>
            </el-table-column>


            <el-table-column prop="time" label="时长" />
            <!-- <el-table-column prop="imgPath" label="文章封面" v-slot="{ row }">
                <el-avatar :src="row.imgPath"></el-avatar> </el-table-column> -->
            <el-table-column :filters="[
                { text: '已审核', value: true },
                { text: '未审核', value: false }

            ]" :filter-method="filterHandler" prop="status" label="是否审核" v-slot="{ row }">
                <el-switch v-model="row.status" class="ml-2" @change="changeStatus(row)"
                    style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" />
            </el-table-column>
            <el-table-column label="操作" v-slot="{ row }">
                <el-button link type="danger" @click="del(row)">删除</el-button>
                <el-button link type="success" @click="getCid(row)">数据完善</el-button>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination v-model:current-page="page" v-model:page-size="pageSize" :page-sizes="[2, 4, 6, 8]" :small="small"
            :disabled="disabled" :background="background" layout="total, sizes, prev, pager, next, jumper" :total=total
            @size-change="handleSizeChange" @current-change="handleCurrentChange" style="margin-top:20px" />

    </el-card>
    <!-- 信息完善框 -->
    <el-dialog v-model="dialogFormVisible" title="视频信息完善" width="500">
        <el-form :model="form">
            <el-form-item label="标题" :label-width="formLabelWidth">
                <el-input v-model="form.title" autocomplete="off" />
            </el-form-item>
            <el-form-item label="封面图" :label-width="formLabelWidth">
                <el-upload class="avatar-uploader" action="http://localhost:3000/kz/uploadImg" :show-file-list="false"
                    :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                    <img v-if="form.img" :src="form.img" class="avatar" />
                    <el-icon v-else class="avatar-uploader-icon">
                        <Plus />
                    </el-icon>
                </el-upload>
            </el-form-item>
            <el-form-item label="时长" :label-width="formLabelWidth">
                <el-input v-model="form.time" autocomplete="off" />
            </el-form-item>
            <el-form-item label="原价" :label-width="formLabelWidth">
                <el-input v-model="form.yprice" autocomplete="off" />
            </el-form-item>
            <el-form-item label="现价" :label-width="formLabelWidth">
                <el-input v-model="form.xprice" autocomplete="off" />
            </el-form-item>
            <el-form-item label="内容" :label-width="formLabelWidth">
                <el-input v-model="form.content" autocomplete="off" />
            </el-form-item>
            <el-form-item label="类型" :label-width="formLabelWidth">
                <el-select v-model="form.type" placeholder="课程类型">
                    <el-option label="vue测试数据" value="vue测试数据" />
                    <el-option label="尚硅谷vue" value="尚硅谷vue" />
                    <el-option label="人工智能" value="人工智能" />
                    <el-option label="Ps平面设计" value="Ps平面设计" />
                </el-select>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogFormVisible = false">Cancel</el-button>
                <el-button type="primary" @click="fillData">
                    OK
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup >
import { onMounted, ref, reactive } from "vue"
import api from "../../utils/request"
import { Plus } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from "vue-router"
const router = useRouter()
const dialogFormVisible = ref(false)
const formLabelWidth = '140px'
const tableData = ref([])
const total = ref()
const page = ref(1)
const pageSize = ref(4)
const searchTitle = ref("")
const searchName = ref("")
const _id = ref("")
const form = reactive({
    title: '',
    img: '',
    time: '',
    type: '',
    yprice: '',
    xprice: '',
    content: '',
    catalog: []
})
onMounted(() => {
    getData()
})

const filterHandler = (
    value,
    row,
    column
) => {
    const property = column['property']
    return row[property] === value
}
//获取数据
const getData = async () => {
    let result = await api.get(`/kz/videoList?page=${page.value}&&pageSize=${pageSize.value}&&searchTitle=${searchTitle.value}&&searchName=${searchName.value}`)
    tableData.value = result.data.data
    total.value = result.data.total
}
// 状态修改
const changeStatus = async (row) => {
    console.log(row);
    let result = await api.put(`/kz/editVideo/${row._id}`, {
        status: row.status
    })
    if (result.data.code === 200) {
        ElMessage.success("审核修改成功")
        getData()

    }
}

//分页
const handleSizeChange = (val) => {
    pageSize.value = val
    getData()
}
const handleCurrentChange = (val) => {
    page.value = val
    getData()
}

//搜索
const search = () => {
    getData()
}

//删除
const del = (row) => {
    ElMessageBox.confirm(
        '是否要删除?',
        'Warning',
        {
            confirmButtonText: 'OK',
            cancelButtonText: 'Cancel',
            type: 'warning',
        }
    )
        .then(() => {
            console.log(11111);
            api.delete(`/kz/delVideo/${row._id}`).then((val) => {
                if (val.data.code === 200) {
                    ElMessage({
                        type: 'success',
                        message: '删除成功',
                    })
                    getData()
                }

            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: 'Delete canceled',
            })
        })
}

//审核页面跳转
const showVideo = (row) => {
    console.log(row);
    router.push({
        path: "/home/videoShow",
        query: row
    });
}
// 
const getCid = (row) => {
    console.log(row);
    dialogFormVisible.value = true
    _id.value = row._id
    form._id = row._id
    form.title = row.title
    form.img = row.img
    form.time = row.time
    form.type = row.type
    form.yprice = row.yprice
    form.xprice = row.xprice
    form.content = row.content
    form.catalog = row.type
    form.videoPath = row.videoPath
    form.buyNum = row.buyNum
}
// 视频数据完善
const fillData = () => {
    dialogFormVisible.value = false
    console.log(form);
    api.put(`/kz/editVideo/${_id.value}`, form).then((val) => {
        if (val.data.code === 200) {
            ElMessage.success("修改成功")
            getData()
        }

    })

}
// 封面图返回值
const handleAvatarSuccess = (val) => {
    console.log(val)
    form.img = val.path
}
</script>

<style lang="less" scoped>
.box-card {
    width: 99%;
}

.el-input {
    width: 230px;
}

.content {
    display: block;
    width: 100px;
    /* 你可以根据需要调整这个值 */
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: #8c939d;
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #9499a1;
    width: 178px;
    height: 178px;
    border: 1px solid #93979d;
    text-align: center;
}
</style>